package com.jeeplus.modules.web.service;

import com.jeeplus.core.persistence.DataEntity;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.web.entity.YjMahjongHallRoom;
import com.jeeplus.modules.web.mapper.YjMahjongHallMapper;
import com.jeeplus.modules.web.mapper.YjMahjongHallRoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 棋牌室房间Service
 * @author liangbing
 * @version 2021-03-12
 */
@Service
@Transactional(readOnly = true)
public class YjMahjongHallRoomService extends CrudService<YjMahjongHallRoomMapper, YjMahjongHallRoom> {
    @Autowired
    private YjMahjongHallMapper yjMahjongHallMapper;

    @Autowired
    private YjMahjongHallRoomMapper yjMahjongHallRoomMapper;

    public YjMahjongHallRoom get(String id) {
        return super.get(id);
    }

    public List<YjMahjongHallRoom> findList(YjMahjongHallRoom yjMahjongHallRoom) {
        return super.findList(yjMahjongHallRoom);
    }

    public Page<YjMahjongHallRoom> findPage(Page<YjMahjongHallRoom> page, YjMahjongHallRoom yjMahjongHallRoom) {
        return super.findPage(page, yjMahjongHallRoom);
    }

    /**
     * 检查二维码是否重复
     * @param yjMahjongHallRoom
     * @return
     */
    public boolean checkQrcodeIsDuplicate(YjMahjongHallRoom yjMahjongHallRoom) {
        return yjMahjongHallRoomMapper.checkQrcodeIsDuplicate(yjMahjongHallRoom) > 0;
    }

    /**
     * 检查棋牌室房间是否重复
     * @param yjMahjongHallRoom
     * @return
     */
    public boolean isDuplicate(YjMahjongHallRoom yjMahjongHallRoom) {
        return yjMahjongHallRoomMapper.isDuplicate(yjMahjongHallRoom) > 0;
    }

    /**
     * 根据二维码获得棋牌室房间信息
     * @param roomQrcode
     * @return
     */
    public YjMahjongHallRoom getRoomByQrcode(String roomQrcode) {
        return yjMahjongHallRoomMapper.getRoomByQrcode(roomQrcode, DataEntity.DEL_FLAG_NORMAL);
    }

    @Transactional(readOnly = false)
    public void save(YjMahjongHallRoom yjMahjongHallRoom) {
        super.save(yjMahjongHallRoom);
    }

    @Transactional(readOnly = false)
    public void saveHallRoom(YjMahjongHallRoom yjMahjongHallRoom){
        if (yjMahjongHallRoom.getIsNewRecord()){

            // 插入房间
            yjMahjongHallRoom.preInsert();
            yjMahjongHallRoomMapper.insert(yjMahjongHallRoom);

            //// 更新棋牌室房间数
            //yjMahjongHallMapper.updateRooms(yjMahjongHallRoom.getMahjongHall().getId(), 1);
        }else{
            yjMahjongHallRoom.preUpdate();
            yjMahjongHallRoomMapper.update(yjMahjongHallRoom);
        }
    }

    @Transactional(readOnly = false)
    public void delete(YjMahjongHallRoom yjMahjongHallRoom) {
        super.delete(yjMahjongHallRoom);
    }

    /**
     * 删除房间
     * @param hallRoomId
     */
    @Transactional(readOnly = false)
    public void deleteRoom(String hallRoomId) {
        // 删除房间
        yjMahjongHallRoomMapper.deleteRoom(hallRoomId);

        //// 更新棋牌室房间数
        //yjMahjongHallMapper.updateRooms(hallId, -1);
    }
}
